<?php
    //判断是否有GET传送地址过来
	if($_GET['url'])
	{
	   $url=$_GET['url'];
	   echo "<p><font color=#009933><b>你使用的是已上传的MP3的地址。接着请设置播放器。<br>建议保留默认，直接点击最下方的生成按钮即可！</b></font></p>";
	  }else{
	  echo "<br><br>";
	  }
	//上传文件
     if($_REQUEST['MAX_FILE_SIZE']){
	    $Filetype=substr(strrchr($_FILES['f']['name'],"."),1);      //获得扩展名
	    if($Filetype=="mp3"||$Filetype=="MP3"||$Filetype=="Mp3"){
	    $stor = new SaeStorage();
        $domain = 'mp3'; 
	    $onlyname=date(Ymj_His);
	    $fileName=$onlyname.".".$Filetype;    //重新命名

        //$fileName = mb_detect_encoding($_FILES['f']['name'])=='UTF-8'?$_FILES['f']['name']:iconv('gbk',"utf-8",$_FILES['f']['name']);
        $filePath = $_FILES['f']['tmp_name'];
		
		/* 判断storage是否快满。若快满则删除最久两首音乐 */
		$stor_size=$stor->getDomainCapacity($domain);//获取已用大小
		if($stor_size>=2*1000*1024*1024)
		{
			$sql="select * from mp3  order by datetime ASC limit 2";  //从数据库中找出最老两首音乐
			$result=mysql_query($sql)OR die("<br>读取数据库错误！<br>");
			if($num=mysql_num_rows($result))
			{
				while($row=mysql_fetch_array($result))
				{
					$delurl=$row['url'];
					$delname=basename($delurl);
					 //打印测试 print_r($row);
					$stor->delete(mp3,$delname);
					$sql="delete  from mp3  where url='$delurl'";
					$result2=mysql_query($sql)OR die("<br>删除数据库错误！<br>");
				}
			}
			
		}
		//开始上传
        $stor->upload($domain,$fileName,$filePath,setAttr($fileName));
        $errmsg=$stor->errmsg();
        if($errmsg==0){
            $url=$stor->getUrl($domain,$fileName);
            echo "<p><font color=#CCCC00><b>文件上传成功！请设置播放器。<br>建议保留默认，直接点击最下方的生产按钮即可！</b></font></p>";
			        }else{
            echo "<p><b>文件上传失败!</b>errmsg:";
            switch ($errmsg) {
                case -2:
                    echo "-2 配额统计错误,请上传10M以内文件";
                    break;
                case -3:
                    echo "-3 权限不足";
                    break;
                case -7:
                    echo "-7 Domain不存在";
                    break;
                case -12:
                    echo "-12 存储服务器返回错误";
                    break;
                case -18:
                    echo "-18 文件不存在";
                    break;
                case -101:
                    echo "-101 参数错误";
                    break;
                case -102:
                    echo "-102 存储服务器连接失败";
                    break;
            }
            echo "</p>";
            
        }
    //写数据库
	  $sql="insert into mp3 set name='".$_FILES['f']['name']."',size='".$_FILES['f']['size']."',datetime=NOW(),url='".$url."' ";
		mysql_query($sql)OR die("<br>写入数据库错误！<br>");
	    }else{
	     	echo "<font color=#FF0000>只允许上传10M以内的MP3文件。</font>";
		 }
}
    
function setAttr($filename){  

//根据文件扩展名强制指定content type 本例将htm文件强制显示为文本

	while($extension=strpos($filename,'.')){  
	$filename=substr($filename,$extension+1);  
	}  

	$type=array('expires' => 'access plus 1 year');

	$mime_types=array();
	$mime_types['htm']    ='text/plain'; //$mime_types['htm']    ='text/html';
	if( strlen($filename) <= 8 ) $type['type'] = $mime_types[$filename];
	return $type ; 
}
//设置背景音乐
if($url)
{
   $bgmusic=$url;
   }
?>